package com.trust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trust.agri.util.BeanUtil;
import com.trust.entity.Checkout;
import com.trust.entity.CheckoutDetail;
import com.trust.entity.VO.CheckoutDetailVO;
import com.trust.entity.VO.CheckoutDropDown;
import com.trust.mapper.CheckoutMapper;
import com.trust.service.CheckoutDetailService;
import com.trust.service.CheckoutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CheckoutServiceImpl extends ServiceImpl<CheckoutMapper, Checkout> implements CheckoutService {

    @Resource
    private CheckoutDetailService checkoutDetailService;

    @Override
    public List<CheckoutDropDown> dropDown() {
        LambdaQueryWrapper<Checkout> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Checkout::getEnabled, 1);
        wrapper.orderByDesc(Checkout::getId);
        List<Checkout> list = this.list(wrapper);
        List<CheckoutDropDown> targetList = BeanUtil.sourceList2targetList(list, CheckoutDropDown.class);
        for (CheckoutDropDown drop : targetList) {
            List<CheckoutDetail> detailList = checkoutDetailService.getByCheckoutId(drop.getId());
            List<CheckoutDetailVO> detailVOS = new ArrayList<>();
            for (CheckoutDetail detail : detailList) {
                CheckoutDetailVO target = BeanUtil.source2target(detail, new CheckoutDetailVO());
                target.setName(detail.getExamineProject());
                target.setStandard(detail.getExamineStandard());
                detailVOS.add(target);
            }
            drop.setDetails(detailVOS);
        }
        return targetList;
    }
}
